import tushare as ts
import json
from flask import Flask,request,jsonify
from helpers import getJsonForData

app = Flask(__name__)

@app.route('/', methods=['GET', 'POST'])
def hello_world():
    return '我是小小股神通'

@app.route('/test', methods=['GET', 'POST'])
def test():
    return 'test'

####################################   交易数据  #############################################

#获取个股历史交易数据
@app.route('/get_hist_data', methods=['GET', 'POST'])
def get_hist_data():
    share_no = request.args.get('share_no', '')
    start_date = request.args.get('start_date', '')
    end_date = request.args.get('end_date', '')
    if share_no=='':
        return jsonify({'status':0,'msg':'缺少share_no'})
    if start_date!='':
        return getJsonForData(ts.get_hist_data(share_no,start=start_date,end=end_date))
    else:
        return getJsonForData(ts.get_hist_data(share_no))

#获取个股全部历史数据
@app.route('/get_h_data', methods=['GET', 'POST'])
def get_h_data():
    share_no = request.args.get('share_no', '')
    start_date = request.args.get('start_date', '')
    end_date = request.args.get('end_date', '')
    if share_no=='':
        return jsonify({'status':0,'msg':'缺少share_no'})
    if start_date!='':
        return getJsonForData(ts.get_h_data(share_no,start=start_date,end=end_date))
    else:
        return getJsonForData(ts.get_h_data(share_no))

#复权数据
@app.route('/get_stock_basics', methods=['GET', 'POST'])
def get_stock_basics():
    return getJsonForData(ts.get_stock_basics())

#一次性获取当前交易所有股票的行情数据
@app.route('/get_today_all', methods=['GET', 'POST'])
def get_today_all():
    return getJsonForData(ts.get_today_all())

####################################   基本面数据  #############################################

#业绩报告
@app.route('/get_report_data', methods=['GET', 'POST'])
def get_report_data():
    year = request.args.get('year', '')
    quarter = request.args.get('quarter', '')
    return getJsonForData(ts.get_report_data(int(year), int(quarter)))

#盈利能力
@app.route('/get_profit_data', methods=['GET', 'POST'])
def get_profit_data():
    year = request.args.get('year', '')
    quarter = request.args.get('quarter', '')
    return getJsonForData(ts.get_profit_data(int(year), int(quarter)))

#营运能力
@app.route('/get_operation_data', methods=['GET', 'POST'])
def get_operation_data():
    year = request.args.get('year', '')
    quarter = request.args.get('quarter', '')
    return getJsonForData(ts.get_operation_data(int(year), int(quarter)))

#成长能力
@app.route('/get_growth_data', methods=['GET', 'POST'])
def get_growth_data():
    year = request.args.get('year', '')
    quarter = request.args.get('quarter', '')
    return getJsonForData(ts.get_growth_data(int(year), int(quarter)))

#偿债能力
@app.route('/get_debtpaying_data', methods=['GET', 'POST'])
def get_debtpaying_data():
    year = request.args.get('year', '')
    quarter = request.args.get('quarter', '')
    return getJsonForData(ts.get_debtpaying_data(int(year), int(quarter)))

#现金流量
@app.route('/get_cashflow_data', methods=['GET', 'POST'])
def get_cashflow_data():
    year = request.args.get('year', '')
    quarter = request.args.get('quarter', '')
    return getJsonForData(ts.get_cashflow_data(int(year), int(quarter)))

####################################   股票分类数据  #############################################

#行业分类
@app.route('/get_industry_classified', methods=['GET', 'POST'])
def get_industry_classified():
    return getJsonForData(ts.get_industry_classified())

#概念分类
@app.route('/get_concept_classified', methods=['GET', 'POST'])
def get_concept_classified():
    return getJsonForData(ts.get_concept_classified())

#地域分类
@app.route('/get_area_classified', methods=['GET', 'POST'])
def get_area_classified():
    return getJsonForData(ts.get_area_classified())

#沪深300成份及权重
@app.route('/get_hs300s', methods=['GET', 'POST'])
def get_hs300s():
    return getJsonForData(ts.get_hs300s())

#中证500成份股
@app.route('/get_zz500s', methods=['GET', 'POST'])
def get_zz500s():
    return getJsonForData(ts.get_zz500s())

#上证50成份股
@app.route('/get_sz50s', methods=['GET', 'POST'])
def get_sz50():
    return getJsonForData(ts.get_sz50s())

####################################   投资参考数据  #############################################

#基金持股
@app.route('/fund_holdings', methods=['GET', 'POST'])
def fund_holdings():
    year = request.args.get('year', '')
    quarter = request.args.get('quarter', '')
    return getJsonForData(ts.fund_holdings(int(year), int(quarter)))

#业绩预告
@app.route('/forecast_data', methods=['GET', 'POST'])
def forecast_data():
    year = request.args.get('year', '')
    quarter = request.args.get('quarter', '')
    return getJsonForData(ts.forecast_data(int(year), int(quarter)))

#分配预案
@app.route('/profit_data', methods=['GET', 'POST'])
def profit_data():
    top = request.args.get('top', 60)
    year = request.args.get('year', 2017)
    return getJsonForData(ts.profit_data(top=top,year=year))

#限售股解禁
@app.route('/xsg_data', methods=['GET', 'POST'])
def xsg_data():
    month = request.args.get('month', 1)
    year = request.args.get('year', 2017)
    return getJsonForData(ts.xsg_data(month=month,year=year))

####################################   宏观经济数据  #############################################

#上证50成份股
@app.route('/get_deposit_rate', methods=['GET', 'POST'])
def get_deposit_rate():
    return getJsonForData(ts.get_deposit_rate())

#贷款利率
@app.route('/get_loan_rate', methods=['GET', 'POST'])
def get_loan_rate():
    return getJsonForData(ts.get_loan_rate())

#国内生产总值
@app.route('/get_gdp_year', methods=['GET', 'POST'])
def get_gdp_year():
    return getJsonForData(ts.get_gdp_year())

#大盘指数行情列表
@app.route('/get_index', methods=['GET', 'POST'])
def get_index():
    return getJsonForData(ts.get_index())

if __name__ == '__main__':
    app.debug = True
    app.run(host='0.0.0.0')

    # res = ts.get_stock_basics()
    # print(res)


